Authors:
Tahmidul Azom Sany (tsany@gmu.edu)
Python is useful in climate science for data analysis, modeling, machine learning, data visualization, and data processing tasks. Its ease of use, extensive libraries, and robust scientific computing capabilities make it a versatile tool for climate scientists to analyze, model, visualize, and communicate climate data and research findings. In this tutorial we will explore some of the basic features of python which will be useful in climate data analysis.
Prerequisite: It is recommended for users to have a basic understanding of the Python programming language before proceeding with this tutorial. This tutorial covers some of the fundamental features (but not all) of Python. Therefore, if you are new to programming, we suggest starting with the following resources:
**Resources for learning Python**
Watch Mikes tutorial: https://www.youtube.com/watch?v=rfscVS0vtbw
Read Blog: 5 Weeks to Mastery: A Beginner’s Guide to Learning Python
1. System Setup
2. Hello World
3. Drawing a shape
4. Variables and Data Type
5. Strings
6. Numbers
7. Input from user
8. Conditional statement
9. Building a basic calculator
10. Function
11. Loops
12. Try and Except
13. Modules and packages
Let's start with our system setup. Open https://colab.research.google.com/
and open File
->New Notebook
. That's all you need to start with Python. No prior installation requried into your local environment.
print("Hello World") # For printing a simple text
friend = 'James'
print(friend + " is Paul's friend") # Approach 01
print(f"{friend} is Paul's friend") # Approach 02 # Find the problem here
print(friend, "is Paul's friend") # Approach 03
You can draw any shape you want with Python.
print(" /|")
print(" / |")
print(" / |")
print(" / |")
print(" / |")
print(" /_____|")
print(" O ")
print(" /|\__#=== --------------------")
print(" / | ")
print(" /|\ ")
print(" / | \ ")
# Variable Naming Rules and Assigning Variable
#Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
#Illegal variable names:
# 2myvar = "John"
# my-var = "John"
# my var = "John"
# Accessing Variable
myvar
# Multiple assignment
a, b = 2,3
sum = a+b
sum
Task: Get a idea of
x = "Hello! This is a text here"
y = 200
type(x)
# What type is the y variable?
a, b = "2", "3"
sum = a+b
sum
# String Concat
x = "Omi"
y = "Loves"
z = "Rain"
print(x + y + z)
name = 'Cristiano Ronaldo'
age = '37'
print( name + ' is the best player in the world.')
print("He is " + age + " Years old")
print("But he doesn't look like "+ age +" Years old")
print("This is first line. \nThis is second line")
phrase = "Ahmed Al Imtiaz" # Ahmed Al Imtiaz is my friend name, You can add your full name Here
print(phrase.lower())
print(phrase.upper())
print(phrase.isupper())
print(phrase.islower())
print(phrase.upper().isupper())
# Operations
length = len(phrase) #to print the length of
first_index = phrase[0] #index start with 0
fourth_index = phrase[3]
# Finding an index
phrase.index("z")
phrase.index("tiaz")
phrase.replace("Imtiaz","Omi")
# Finding type
a = 100
b = 10.22
c = True
d = [0,1,2,3,4]
type(a)
type(b)
type(c)
type(d)
# Basic Calculations
print(3+2)
print(10%3) #finding a remainder
print(40/2) #division always returns a flaot
# Converting number into string
num = 55
num_string = str(num)
type(num_string)
# Operation
num = -2
absolute_value = abs(num) #returns absolute value
power_num = pow(4,2) #returns power 4^2
max_num = max(4,5,6,1,2,3)
min_num = min(1,3,2,5,7,2,3)
round_num = round(5.3)
# math package
from math import *
a = floor(3.4)
b = ceil(3.4)
square_root = sqrt(16)
exponential = exp(10)
ln_value = log(2) # Natural Logarithm (Ln)
log_value = log10(2) # Base-10 Logarithm (Log)
print(ln_value) # Print other values
Name = input("Enter your name: ")
Age = int(input("Enter your age: "))
# print("Welcome "+ Name + "! You are " + Age+ " years old" )
"""
I woke up
If I am hungry
I eat food
Going to university
If it is raining
I will bring an umbrella
otherwise
I will not bring an umbrella
"""
# Structure
"""
if condition:
____exicute this line
else:
____this line"""
# Basic If Statement
is_male = False #try with False
is_tall = True
# if is_male or is_tall:
# print("You are a male or a tall or both")
if is_male and is_tall:
print("You are a tall male")
elif is_male and not(is_tall):
print("You are a short male")
elif not(is_male) and is_tall:
print("You are a tall but not a male")
else:
print("You are neither male nor tall")
# Comparison
a = 2
b = 3
if a>b:
print("a is bigger")
if a==b:
print("a is equal to b")
else:
print("b is bigger")
num1 = float(input("Enter first number: "))
op = input("Enter operation: ")
num2 = float(input("Enter second number: "))
if op == '+':
print(num1+num2)
elif op == '-':
print(num1-num2)
elif op == '*':
print(num1*num2)
elif op == '/':
print(num1/num2)
else:
print("Invalid Operation! ")
- Why didn't we use int?
- Why did we we use float?
# Structure
def function_name(variables): #could be multiple variable
statement
def my_function():
print("Function is called")
my_function()
def best_player(name):
print(f"{name} is the best player.")
best_player("Cristiano Ronaldo")
def sumation(a,b,c):
print(a+b+c)
sumation(1,2,3)
i = 1
while i<10:
print(i)
i = i+1 # Approach 01
# i += 1 # Approach 02
for i, letter in enumerate("BD Climate"):
print(i, letter)
for number in range(10):
print(number) #why didn't it print 10
for number in range(3, 10, 3):
print(number)
# task is to get the sum of every odd number from 1 to 33(inclusive)
student = ['Omi', 'Imtiaz', 'Tuaha']
for i in range(len(student)):
print(student[i])
for index in range(5):
if index==0:
print("First iteration")
else:
print("Not First")
# Nested loop
color = ["Red", "Black", "Blue"]
car = ["BMW", "Marcedes", "Toyota"]
for value in color:
for another_value in car:
print(value, another_value)
# print(3**2)
# we need to do this in loopic way
def to_the_power(base_number, power):
result = 1
for number in range(power):
result = result * base_number
return result
print(to_the_power(2,3))
# 2D list
number_matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
number_matrix[0][2]
# task is to make a 1D list from the above list.
# 1D list looks like [1,2,3.....0]
try:
num = int(input("Enter a number: "))
print(num)
except:
print("Invalid Number")
In the next chapter we will deal with NumPy, Pandas and Matplotlib.